﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio.Shell.Interop;
//using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace VS2010.TFSHelperPackage
{
    class WitDocumentClient
    {
        public static void ShowCurrentWitDocumentDetails(IServiceProvider serviceProvider)
        {
            IWorkItemTrackingDocument doc = null;

            try
            {
                doc = WitDocumentHelper.GetCurrentWitDocument(serviceProvider);
                if (doc == null)
                {
                    ShowMessage(serviceProvider, "No Workitem tracking document is current");
                    return;
                }

                string message = String.Empty;
                if (doc is IResultsDocument)
                {
                    int cnt = ((IResultsDocument)doc).SelectedItemIds.Length;
                    if (cnt > 0)
                    {
                        string selectedItems = string.Join(",", ((IResultsDocument)doc).SelectedItemIds);
                        message = String.Format("Results document with {0} items selected in server {1} workitem id {2}",
                            ((IResultsDocument)doc).SelectedItemIds.Length,
                            doc.TeamFoundationServer.Name,
                            selectedItems);
                    }
                    else
                    {
                        message = String.Format("Results document with {0} items selected in server {1} workitem id {2}",
                           ((IResultsDocument)doc).SelectedItemIds.Length,
                           doc.TeamFoundationServer.Name,
                           ((IResultsDocument)doc).SelectedItemIds[0]);
                    }
                }
                else if (doc is IWorkItemDocument)
                {
                    message = String.Format("Work Item Document id {0} in server {1}",
                        ((IWorkItemDocument)doc).Item.Id,
                        doc.TeamFoundationServer.Name);
                }
                else if (doc is IQueryDocument)
                {
                    message = String.Format("Query document {0} with {1} items selected in server {2}",
                        ((IQueryDocument)doc).Name,
                        ((IQueryDocument)doc).SelectedItemIds.Length,
                        doc.TeamFoundationServer.Name);
                }
                else
                {
                    Debug.Fail("Unknown document type");
                    message = "Unknown work item tracking document";
                }
                ShowMessage(serviceProvider, message);
            }
            finally
            {
                if (doc != null)
                    doc.Release(serviceProvider);
            }
        }

        private static void ShowMessage(IServiceProvider serviceProvider, string message)
        {
            // Show a Message Box to prove we were here
            IVsUIShell uiShell = (IVsUIShell)serviceProvider.GetService(typeof(SVsUIShell));
            Guid clsid = Guid.Empty;
            int result;
            uiShell.ShowMessageBox(
                       0,
                       ref clsid,
                       "WorkItemTrackingCustomizationSample",
                       message,
                       string.Empty,
                       0,
                       OLEMSGBUTTON.OLEMSGBUTTON_OK,
                       OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                       OLEMSGICON.OLEMSGICON_INFO,
                       0,        // false
                       out result);
        }
    }
}
